<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>志愿者管理</el-breadcrumb-item>
      <el-breadcrumb-item>志愿者申请列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-form :inline="true">
        <el-form-item label="用户名称">
          <el-input placeholder="请输申请用户名称" clearable v-model="uName" @clear="search">
          </el-input>
        </el-form-item>
        <el-form-item label="申请时间">
          <div class="block">
            <el-date-picker
                v-model="applyTime"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择申请时间"
                @clear="search"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="applyList" border stripe>
        <el-table-column label="申请编号" prop="id"></el-table-column>
        <el-table-column label="用户名称" prop="userName"></el-table-column>
        <el-table-column label="申请理由" prop="message"></el-table-column>
        <el-table-column label="申请时间" prop="insertTime"></el-table-column>
        <el-table-column label="审核状态" prop="status">
          <template v-slot="sc">
            <span v-if="sc.row.status == 0">正在审核</span>
            <span v-else-if="sc.row.status == 1" style="color: #67C23A">审核通过</span>
            <span v-else-if="sc.row.status == 2" style="color: #F56C6C">审核未通过</span>
            <span v-else-if="sc.row.status == -1" style="color: #1838fc">用户取消申请</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="mini" v-if="scope.row.status == 0" @click="agree(scope.row.id)">同意
            </el-button>
            <el-button type="danger" size="mini" v-if="scope.row.status == 0" @click="disagree(scope.row.id)">不同意
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Apply",
  data() {
    return {
      //分页
      pageNum: 1,
      pageSize: 3,
      total: 0,
      //搜索条件表单
      uName: '',
      applyTime: '',
      //申请表
      applyList: [],
    }
  },
  methods: {
    //============操作=============
    //同意
    agree(id) {
      this.$confirm('是否确认审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeStatus(id, 1);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    //不同意
    disagree(id) {
      this.$confirm('是否确认审核不通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeStatus(id, 2);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    changeStatus(id, status) {
      let url = "/apply/changStatus/" + id + "/" + status;
      this.$axios.put(url)
          .then((res) => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: res.data.msg,
                duration: 1000
              });
              this.initApplyList();
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg,
                duration: 1000
              });
            }
          })
    },
    //============搜索分页=============
    //每页显示数量变化时
    handleSizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1;
      this.initApplyList();
    },
    //点击跳转页面
    handleCurrentChange(now) {
      this.pageNum = now;
      this.initApplyList();
    },
    //搜索
    search() {
      this.pageNum = 1;
      this.initApplyList();
    },
    //============加载数据=============
    //加载申请列表
    initApplyList() {
      let url = "/apply/applyList";
      this.$axios.get(url, {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.uName,
          time: this.applyTime,
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.applyList = res.data.data.list;
          this.pageSize = res.data.data.pageSize;
          this.pageNum = res.data.data.pageNum;
          this.total = res.data.data.total;
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 1000
          })
        }
      })
    },
  },
  created() {
    this.initApplyList();
  }
}
</script>

<style scoped>

</style>